package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Override
    public PageResponse<NursingProjectVo> getByPage(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        Page<NursingProject>  projects=nursingProjectMapper.selectById(name,status);
        return PageResponse.of(projects, NursingProjectVo.class);

    }

    @Override
    public void add(NursingProjectDto nursingProjectDto) {
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.insertNursingProject(bean);
    }

    @Override
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectNursingProjectById(id);
        NursingProjectVo nursingProjectVo = new NursingProjectVo();
        BeanUtils.copyProperties(nursingProject,nursingProjectVo);
        return nursingProjectVo;
    }

    @Override
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.updateNursingProject(bean);
    }

    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    @Override
    public void deleteById(Long id) {
        nursingProjectMapper.deleteNursingProjectById(id);
    }

    @Override
    public List<NursingProjectVo> get() {
        List<NursingProject> list=nursingProjectMapper.selectAllNursingProjects();
        List<NursingProjectVo> nursingProjectVos = BeanUtil.copyToList(list, NursingProjectVo.class);
        return nursingProjectVos;
    }
}
